package com.meitianhui.convenitenbuy.module.home.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.module.home.model.bean.HomeGoodsInfo;
import com.meitianhui.convenitenbuy.util.GlideUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

/**
 * 拼团领
 * Created by Administrator on 2017/11/3.
 */
public class HomeFightGroupAdapter
        extends BaseQuickAdapter<HomeGoodsInfo.Convient, BaseViewHolder> {

    private Context context;
    private HashMap<String, String> map;

    public HomeFightGroupAdapter(Context context, List data, HashMap<String, String> map) {
        super(R.layout.item_home_fight_group_goods_item, data);
        this.context = context;
        this.map = map;
    }

    @Override
    protected void convert(BaseViewHolder helper, HomeGoodsInfo.Convient item) {
        ImageView ivStorePic = helper.getView(R.id.iv_fight_group);
        ImageView ivStoreSellOut = helper.getView(R.id.iv_fight_group_sell_out);
        TextView tvCostPrice = helper.getView(R.id.tv_cost_price);
        String url;
        try {
            JSONArray jsonArray = new JSONArray(item.getPic_info());
            if (jsonArray.length() > 0) {
                JSONObject jsonObject = jsonArray.getJSONObject(0);
                url = jsonObject.getString("path_id");
                GlideUtils.setRectImage(context, map.get(url),
                        R.mipmap.icon_default_picture, R.mipmap.icon_default_picture, ivStorePic);
                int saleQty = Integer.parseInt(item.getSale_qty());
                if (saleQty <= 0) {
                    ivStoreSellOut.setVisibility(View.VISIBLE);
                } else {
                    ivStoreSellOut.setVisibility(View.GONE);
                }
                helper.setText(R.id.tv_title, item.getTitle());
                helper.setText(R.id.tv_pay_price, "¥" + item.getDiscount_price());
                tvCostPrice.setText("原价¥" + item.getMarket_price());
                tvCostPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                helper.setText(R.id.tv_people, item.getTs_min_num() + "人即可成团");

                TextView label1 = helper.getView(R.id.tv_label1);
                TextView label2 = helper.getView(R.id.tv_label2);
                TextView label3 = helper.getView(R.id.tv_label3);

                String label = item.getLabel();
                if (!TextUtils.isEmpty(label)) {
                    String[] split = label.split(",");
                    if (split.length == 3) {
                        label1.setVisibility(View.VISIBLE);
                        label2.setVisibility(View.VISIBLE);
                        label3.setVisibility(View.VISIBLE);
                        label1.setText(split[0]);
                        label2.setText(split[1]);
                        label3.setText(split[2]);
                    } else if (split.length == 2) {
                        label1.setVisibility(View.VISIBLE);
                        label2.setVisibility(View.VISIBLE);
                        label3.setVisibility(View.GONE);
                        label1.setText(split[0]);
                        label2.setText(split[1]);
                    } else if (split.length == 1) {
                        label1.setVisibility(View.VISIBLE);
                        label2.setVisibility(View.GONE);
                        label3.setVisibility(View.GONE);
                        label1.setText(split[0]);
                    }
                } else {
                    label1.setVisibility(View.GONE);
                    label2.setVisibility(View.GONE);
                    label3.setVisibility(View.GONE);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
